import { ref, computed } from "vue";
import { defineStore } from "pinia";
export const useFormStore = defineStore(
  "form",
  () => {
    const moduleKey: any[] = [];
    const list = ref([]);
    const onListAdd = (arr: any[]) => {
      list.value = arr;
    };
    const clearList = () => {
      list.value = [];
    };

    // 递归获取配置
    const appConfig = ref({});
    function appRecurve(arr = []) {
      if (arr.length === 0) {
        return {};
      }
      let obj = {};
      arr.forEach((item) => {
        obj[item.id] = item;
        if (item.children && item.children.length > 0) {
          obj = { ...obj, ...appRecurve(item.children) };
        }
      });
      return obj;
    }
    const appConfigAdd = (arr) => {
      appConfig.value = appRecurve(arr);
    };
    return {
      moduleKey,
      list,
      appConfig,
      appRecurve,
      appConfigAdd,
      onListAdd,
      clearList,
    };
  },
  {
    persist: {
      paths: ["moduleKey", "list"],
    },
  }
);
